package Client; //done

import Entities.*;

import javax.swing.JOptionPane;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.Date;

/**
 * This class controls the connection to the server
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class ConnectionToServer {
	
	/** The CCRM Client Controller. */
	static private CCRM_Client_Controller ccrm_Client_Controller = null;

	/**
	 * Default Constructor
	 */
	public ConnectionToServer(){
		super();		
	}
		
	/**
	 * @return the client
	 */
	public static synchronized final CCRM_Client_Controller getClient(){
		return ccrm_Client_Controller;
	}
	
	/**
	 * This function sends a message to server and waits for
	 * a returned message from the server
	 * @param msgToserver
	 * @return message from server on success, null on fail
	 */
	private synchronized final Object sendAndGetFromServer(String[] msgToserver){
		ccrm_Client_Controller.handleMessageFromClientUI(msgToserver);
		try{
			int count = 0;
			while (!ccrm_Client_Controller.checkIfClientCanRead()){
				Thread.sleep(300);
				count++;
				if (count == 10){
					JOptionPane.showMessageDialog(null, "Server is not responding!!");
					return null;
				}					
			}				
		}
		catch(InterruptedException exception){
			JOptionPane.showMessageDialog(null, "Error!!");
			return null;
		}
		return ccrm_Client_Controller.getMessagefromserver();
	}
	 

	/**
	 * This function sets a static client
	 * @param newclient
	 */
	public static synchronized final void setClient(CCRM_Client_Controller new_ccrm_Client){
		if (ccrm_Client_Controller == null)
			ccrm_Client_Controller = new_ccrm_Client;		
	}
	
	/**
	 * This function sends a message to server to check if user
	 * details are correct, or if he is already online
	 * @param username The login username
	 * @param password The login password
	 * @return server message on success, null on fail
	 */
	public synchronized Object Login(String username, char[] password){
		String[] login = new String[3];
		String pass = "";
		int i, passwordlength;

		login[0] = "Login";
		login[1] = username;
			
		passwordlength = password.length;
		try{
			for (i=0; i < passwordlength; i++)
				pass = pass + password[i];
		}
		catch(Exception exception){
			return null;
		}
		login[2] = pass;
		return (sendAndGetFromServer(login));		
	}
	
	/**
	 * This function sends a message to server to log 
	 * a user out of the system
	 * @return servers message
	 */
	public synchronized Object LogOut(){
		String[] logout = new String[1];  
		logout[0] = "LogOut";
		return (sendAndGetFromServer(logout));				
	}	
	
	/**
	 * This function sends a message to server to load a product
	 * @param prodID The product ID
	 * @return server message
	 */
	public synchronized Object loadProduct(long prodID){

		String[] loadproduct = new String[2];
		loadproduct[0] = "LoadProduct";
		loadproduct[1] = String.valueOf(prodID);
		return (sendAndGetFromServer(loadproduct));				
	}

	/**
	 * This function sends a message to server to add a new product
	 * @param product The added product
	 * @return server message on success, null on fail
	 */
	public synchronized Object addProduct(Product product){
		String[] prod = product.toArrString();
		if (prod != null){
			String[] addproduct = new String[6];
			addproduct[0] = "AddProduct";
			addproduct[1] = prod[0];
			addproduct[2] = prod[1];
			addproduct[3] = prod[2];
			addproduct[4] = prod[3];
			addproduct[5] = prod[4];
			return (sendAndGetFromServer(addproduct));
		}
		return null;
	}
	
	/**
	 * This function sends a message to server to update product
	 * details on DB
	 * @param product The product to update
	 * @return server message on success, null on fail
	 */
	public synchronized Object updateProduct(Product product){
		String[] prod = product.toArrString();
		String[] updateproduct = new String[6];
		if (prod != null){
			updateproduct[0] = "UpdateProduct";
			updateproduct[1] = prod[0];
			updateproduct[2] = prod[1];
			updateproduct[3] = prod[2];
			updateproduct[4] = prod[3];
			updateproduct[5] = prod[4];
			return (sendAndGetFromServer(updateproduct));				
		}
		return null;		
	}
	
	/**
	 * 	This function sends a message to server to remove a product from DB
	 * @param ProdID
	 * @return servers message
	 */
	public synchronized Object removeProduct(Long ProdID){
		String[] removeproduct = new String[2];  
		removeproduct[0] = "RemoveProduct";
		removeproduct[1] = String.valueOf(ProdID);
		return (sendAndGetFromServer(removeproduct));				
	}
	
	/**
	 * This function sends a message to server to load all
	 * products IDs from DB
	 * @return servers message
	 */
	public synchronized Object loadAllProductsID(){
		String[] loadallproductsID = new String[1];  
		loadallproductsID[0] = "LoadAllProductsID";
		return (sendAndGetFromServer(loadallproductsID));				
	}
	
	/**
	 * This function sends a message to server to load all
	 * products names from DB
	 * @return servers message
	 */
	public synchronized Object loadAllProductsNames(){
		String[] loadallproductsNames = new String[1];  
		loadallproductsNames[0] = "LoadAllProductsNames";
		return (sendAndGetFromServer(loadallproductsNames));				
	}	
	
	/**
	 * This function sends a message to servet to load 
	 * a customer from DB
	 * @param custID The customer ID
	 * @return server message
	 *
	 */
	public synchronized Object loadCustomer(long custID){
		String[] loadcustomer = new String[2];
		loadcustomer[0] = "LoadCustomer";
		loadcustomer[1] = String.valueOf(custID);
		return (sendAndGetFromServer(loadcustomer));				
	}
	
	/**
	 * This function sends a message to server to load 
	 * all customers from the DB
	 * @return server message
	 */
	public synchronized Object loadAllCustomers(){
		String[] loadallcustomers = new String[1];
		loadallcustomers[0] = "LoadAllCustomers";
		return (sendAndGetFromServer(loadallcustomers));				
	}
	
	/**
	 * This function sends a message to server to add
	 * a new customer to DB
	 * @param customer The added customer
	 * @return server message
	 */
	public synchronized Object addCustomer(Customer customer){
		String[] cust = customer.toTheArrString();
		if (cust != null){
			String[] addcustomer = new String[6];
			addcustomer[0] = "AddCustomer";
			addcustomer[1] = cust[0];
			addcustomer[2] = cust[1];
			addcustomer[3] = cust[2];
			addcustomer[4] = cust[3];
			addcustomer[5] = cust[4];
			
			return (sendAndGetFromServer(addcustomer));
		}
		return null;
	}
	
	/**
	 * This function sends a message to server to update customer 
	 * details on the DB
	 * @param customer The customer to update
	 * @return server message
	 */
	public synchronized Object updateCustomer(Customer customer){
		String[] cust = customer.toTheArrString();
		String[] updatecustomer = new String[6];
		if (cust != null){
			updatecustomer[0] = "UpdateCustomer";
			updatecustomer[1] = cust[0];
			updatecustomer[2] = cust[1];
			updatecustomer[3] = cust[2];
			updatecustomer[4] = cust[3];
			updatecustomer[5] = cust[4];
			return (sendAndGetFromServer(updatecustomer));				
		}
		return null;		
	}
	
	/**
	 * This function sends a message to server to load 
	 * a template from DB
	 * @param tempID The template ID
	 * @return server message
	 */
	public synchronized Object loadTemplate(long tempID){
		String[] loadtemplate = new String[2];
		loadtemplate[0] = "LoadTemplate";
		loadtemplate[1] = String.valueOf(tempID);
		return (sendAndGetFromServer(loadtemplate));				
	}
	
	/**
	 * This function sends a message to server to load all 
	 * templates from DB 
	 * @return server message
	 */
	public synchronized Object loadAllTemplates(){
		String[] loadalltemplates = new String[1];
		loadalltemplates[0] = "LoadAllTemplates";
		return (sendAndGetFromServer(loadalltemplates));				
	}
	
	/**
	 * This function sends a message to server to create
	 * a new Template on DB
	 * @param template The template to create
	 * @return servers message
	 */
	public synchronized Object createTemplate(Template template){
		String[] createtemplate = new String[6];
		createtemplate[0] = "CreateTemplate";
		createtemplate[1] = String.valueOf(template.getTemplateID());
		createtemplate[2] = template.getTemplateName();
		createtemplate[3] = template.getClientmarket();
		createtemplate[4] = template.getMsg();	
		createtemplate[5] = String.valueOf(template.getProduct().getProdID());
		return (sendAndGetFromServer(createtemplate));				
	}
	
	/**
	 * This function sends a message to server to update template
	 * details on the DB
	 * @param template The template to update
	 * @return servers message
	 */
	public synchronized Object updateTemplate(Template template){
		String[] campaindetails = new String[6];
		campaindetails[0] = "UpdateTemplate";
		campaindetails[1] = String.valueOf(template.getTemplateID());
		campaindetails[2] = template.getTemplateName();
		campaindetails[3] = template.getClientmarket();
		campaindetails[4] = template.getMsg();	
		campaindetails[5] = String.valueOf(template.getProduct().getProdID());
		return (sendAndGetFromServer(campaindetails));				
	}
	
	/**
	 * This function sends a message to server to load all
	 * address zones
	 * @return servers message
	 */
	public synchronized Object loadAllAddressZones(){
		String[] loadalladdresszones = new String[1];  
		loadalladdresszones[0] = "loadAllAddressZones";
		return (sendAndGetFromServer(loadalladdresszones));				
	}	
	
	/**
	 * This function sends a message to server to add 
	 * a new address zone to DB
	 * @param AddressZoneID The address zone ID
	 * @param AddressZoneName The address zone name
	 * @return servers message
	 */
	public synchronized Object addAddressZone(int AddressZoneID, String AddressZoneName){
		String[] addaddresszone = new String[3];  
		addaddresszone[0] = "AddAddressZone";
		addaddresszone[1] = String.valueOf(AddressZoneID);
		addaddresszone[2] = AddressZoneName;
		return (sendAndGetFromServer(addaddresszone));				
	}	
	
	/**
	 * This function sends a message to server to update
	 * address zone on the DB
	 * @param AddressZoneID The address zone ID
	 * @param AddressZoneName The address zone Name
	 * @return servers message
	 */
	public synchronized Object updateAddressZone(int AddressZoneID, String AddressZoneName){
		String[] updateaddresszone = new String[3];  
		updateaddresszone[0] = "UpdateAddressZone";
		updateaddresszone[1] = String.valueOf(AddressZoneID);
		updateaddresszone[2] = AddressZoneName;
		return (sendAndGetFromServer(updateaddresszone));				
	}	
	
	/**
	 * This function sends a message to server to load
	 * a deal sale from DB
	 * @param DealSaleID The deal sale ID to load 
	 * @return servers message
	 */
	public synchronized Object loadDealSale(long DealSaleID){
		String[] loaddealsale = new String[2];  
		loaddealsale[0] = "LoadDealSale";
		loaddealsale[1] = String.valueOf(DealSaleID);
		return (sendAndGetFromServer(loaddealsale));				
	}	
	
	/**
	 * This function sends a message to server to load all
	 * deal sales from DB
	 * @return servers message
	 */
	public synchronized Object loadAllDealSales(){
		String[] loadalldealsales = new String[1];  
		loadalldealsales[0] = "LoadAllDealSales";
		return (sendAndGetFromServer(loadalldealsales));				
	}	
	
	/**
	 * This function sends a message to server to save
	 * a new deal sale to DB
	 * @param dealsale The new deal sale to save
	 * @return servers message
	 */
	public synchronized Object saveDealSale(DealSale dealsale){
		String[] savedealsale = new String[6]; 
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		String DateAndTime;
		savedealsale[0] = "SaveDealSale";
		savedealsale[1] = String.valueOf(dealsale.getDealSaleID());
		DateAndTime = dateFormat.format(dealsale.getStartDate());
		savedealsale[2] = DateAndTime;
		DateAndTime = dateFormat.format(dealsale.getEndDate());
		savedealsale[3] = DateAndTime;
		savedealsale[4] = dealsale.getStatus();
		savedealsale[5] = String.valueOf(dealsale.getTemplate().getTemplateID());
		return (sendAndGetFromServer(savedealsale));				
	}
	
	/**
	 * This function sends a message to server to update 
	 * deal sale details on DB
	 * @return servers message
	 */
	public synchronized Object updateDealSale(DealSale dealsale){
		String[] msg = new String[6];  
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		String datetime;
        msg[0] = "UpdateDealSale";
		msg[1] = String.valueOf(dealsale.getDealSaleID());
		datetime = dateFormat.format(dealsale.getStartDate());
		msg[2] = datetime;
		datetime = dateFormat.format(dealsale.getEndDate());
		msg[3] = datetime;
		msg[4] = dealsale.getStatus();
		return (sendAndGetFromServer(msg));				
	}
	
	/**
	 * This function sends a message to server to load
	 * all customer interests from DB
	 * @param CustID The customer ID
	 * @return servers message
	 */
	public synchronized Object LoadAllCustomerInterests(long CustID){
		String[] loadAllCustomerInterests = new String[2];
		loadAllCustomerInterests[0] = "LoadAllCustomerInterests";
		loadAllCustomerInterests[1] = String.valueOf(CustID);
		return (sendAndGetFromServer(loadAllCustomerInterests));
	}
	
	/**
	 * This function sends a message to server to add
	 * customer interest to DB
	 * @param CustID The customer ID
	 * @return servers message
	 */
	public synchronized Object AddCustomerInterest(long CustID, int CatID, int Rate){
		String[] addCustomerInterest = new String[4];
		addCustomerInterest[0] = "AddCustomerInterest";
		addCustomerInterest[1] = String.valueOf(CustID);
		addCustomerInterest[2] = String.valueOf(CatID);
		addCustomerInterest[3] = String.valueOf(Rate);
		return (sendAndGetFromServer(addCustomerInterest));
	}
	
	/**
	 * This function sends a message to server to delete
	 * customer interest from DB
	 * @param CustID The customer ID
	 * @return servers message
	 */
	public synchronized Object DeleteCustomerInterest(long CustID, int CatID){
		String[] addCustomerInterest = new String[3];
		addCustomerInterest[0] = "DeleteCustomerInterest";
		addCustomerInterest[1] = String.valueOf(CustID);
		addCustomerInterest[2] = String.valueOf(CatID);
		return (sendAndGetFromServer(addCustomerInterest));
	}
	
	/**
	 * This function sends a message to server to load
	 * all Categories of a Product from DB
	 * @param prodID The product ID to load
	 * @return servers message
	 */
	public synchronized Object loadAllCategoriesOfProduct(long prodID){
		String[] loadAllCategoriesOfProduct = new String[2];  
		loadAllCategoriesOfProduct[0] = "LoadAllCategoriesOfProduct";
		loadAllCategoriesOfProduct[1] =  String.valueOf(prodID);
		return (sendAndGetFromServer(loadAllCategoriesOfProduct));				
	}	

	/**
	 * This function sends a message to server to load
	 * all Categories from DB
	 * @return servers message
	 */
	public synchronized Object loadAllCategories(){
		String[] loadAllCategories = new String[1];  
		loadAllCategories[0] = "LoadAllCategories";
		return (sendAndGetFromServer(loadAllCategories));				
	}	
	
	/**
	 * This function sends a message to server to load 
	 * all Category Products from DB
	 * @param categoryID The Category ID to load
	 * @return servers message
	 */
	public synchronized Object loadCategoryProducts(int categoryID){
		String[] loadCategoryProducts = new String[2];  
		loadCategoryProducts[0] = "LoadCategoryProducts";
		loadCategoryProducts[1] = String.valueOf(categoryID);
		return (sendAndGetFromServer(loadCategoryProducts));				
	}	
	
	/**
	 * This function sends a message to server to add 
	 * a new Category to DB
	 * @param categoryID The category ID to add
	 * @param categoryName The category Name to add
	 * @return servers message
	 */
	public synchronized Object addCategory(int categoryID, String categoryName){
		String[] addCategory = new String[3];  
		addCategory[0] = "AddCategory";
		addCategory[1] = String.valueOf(categoryID);
		addCategory[2] = categoryName;
		return (sendAndGetFromServer(addCategory));				
	}	
	
	/**
	 * This function sends a message to server to update
	 * Category details on DB
	 * @param categoryID The category ID to update
	 * @param categoryName The category Name to update
	 * @return servers message
	 */
	public synchronized Object updateCategory(int categoryID, String categoryName){
		String[] updateCategorySection = new String[3];  
		updateCategorySection[0] = "UpdateCategory";
		updateCategorySection[1] = String.valueOf(categoryID);
		updateCategorySection[2] = categoryName;
		return (sendAndGetFromServer(updateCategorySection));				
	}	
	
	/**
	 * This function sends a message to server to update
	 * Category details on DB
	 * @param categoryID The category ID to update
	 * @return servers message
	 */
	public synchronized Object removeCategory(int categoryID){
		String[] removeCategory = new String[2];  
		removeCategory[0] = "RemoveCategory";
		removeCategory[1] = String.valueOf(categoryID);
		return (sendAndGetFromServer(removeCategory));				
	}	
	
	/**
	 * This function sends a message to server to add
	 * a new Type to DB
	 * @param typeID The type ID to add
	 * @param typeName The type Name to add
	 * @return servers message
	 */
	public synchronized Object addType(int typeID, String typeName){
		String[] addType = new String[3];  
		addType[0] = "AddType";
		addType[1] = String.valueOf(typeID);
		addType[2] = typeName;
		return (sendAndGetFromServer(addType));				
	}	
	
	/**
	 * This function sends a message to server to add
	 * a new Type to DB
	 * @param typeID The type ID to update
	 * @param typeName The type Name to update 
	 * @return servers message
	 */
	public synchronized Object updateType(int typeID, String typeName){
		String[] updateType = new String[3];  
		updateType[0] = "UpdateType";
		updateType[1] = String.valueOf(typeID);
		updateType[2] = typeName;
		return (sendAndGetFromServer(updateType));				
	}	
	
	/**
	 * This function sends a message to server to add
	 * a new Type to DB
	 * @param typeID The type ID to remove
	 * @param typeName The type Name to remove
	 * @return servers message
	 */
	public synchronized Object removeType(int typeID){
		String[] removeType = new String[2];  
		removeType[0] = "RemoveType";
		removeType[1] = String.valueOf(typeID);		
		return (sendAndGetFromServer(removeType));				
	}
	
	/**
	 * This function sends a message to server to load
	 * all products that are related to this type from DB
	 * @param typeID The type ID to load 
	 * @return servers message
	 */
	public synchronized Object loadTypeProduct(int typeID){
		String[] loadTypeProduct = new String[2];  
		loadTypeProduct[0] = "LoadTypeProduct";
		loadTypeProduct[1] = String.valueOf(typeID);
		return (sendAndGetFromServer(loadTypeProduct));				
	}	
	
	/**
	 * This function sends a message to server to add
	 * a product to type in the DB
	 * @param typeID The type ID to add product to
	 * @param productID The product ID to add to type 
	 * @return servers message
	 */
	public synchronized Object addProductToType(int typeID, long productID){
		String[] addProductToType = new String[3];  
		addProductToType[0] = "AddProductToType";
		addProductToType[1] = String.valueOf(typeID);
		addProductToType[2] = String.valueOf(productID);
		return (sendAndGetFromServer(addProductToType));				
	}	
	
	/**
	 * This function sends a message to server to remove
	 * a product to type in the DB
	 * @param typeID The type ID to remove product from
	 * @param productID The product ID to remove from type 
	 * @return servers message
	 */
	public synchronized Object removeProductFromType(int typeID, long productID){
		String[] removeProductFromType = new String[3];  
		removeProductFromType[0] = "RemoveProductFromType";
		removeProductFromType[1] = String.valueOf(typeID);
		removeProductFromType[2] = String.valueOf(productID);
		return (sendAndGetFromServer(removeProductFromType));				
	}	
	
	/**
	 * This function sends a message to server to add
	 * type to category in the DB
	 * @param categoryID The category ID to add type to
	 * @patam typeID The type ID to add
	 * @return servers message
	 */
	public synchronized Object addTypeToCategory(int categoryID, int typeID){
		String[] addTypeToCategory = new String[3];  
		addTypeToCategory[0] = "AddTypeToCategory";
		addTypeToCategory[1] = String.valueOf(categoryID);
		addTypeToCategory[2] = String.valueOf(typeID);
		return (sendAndGetFromServer(addTypeToCategory));				
	}	
	
	/**
	 * This function sends a message to server to remove
	 * type from category in the DB
	 * @param categoryID The category ID to remove type from
	 * @patam typeID The type ID to remove
	 * @return servers message
	 */
	public synchronized Object removeTypeFromCategory(int categoryID, int typeID){
		String[] removeTypeFromSection = new String[3];  
		removeTypeFromSection[0] = "RemoveTypeFromCategory";
		removeTypeFromSection[1] = String.valueOf(categoryID);
		removeTypeFromSection[2] = String.valueOf(typeID);
		return (sendAndGetFromServer(removeTypeFromSection));				
	}	
	
	/**
	 * This function sends a message to server to load
	 * all category types from DB
	 * @param categoryID The category ID to load
	 * @return servers message
	 */
	public synchronized Object loadAllCategoryTypes(int categoryID){
		String[] loadAllCategoryTypes = new String[2];  
		loadAllCategoryTypes[0] = "LoadAllCategoryTypes";
		loadAllCategoryTypes[1] = String.valueOf(categoryID);
		return (sendAndGetFromServer(loadAllCategoryTypes));				
	}	
	
	/**
	 * This function sends a message to server to load
	 * all types from DB 
	 * @return servers message
	 */
	public synchronized Object loadAllTypes(){
		String[] loadAllTypes = new String[1];  
		loadAllTypes[0] = "LoadAllTypes";
		return (sendAndGetFromServer(loadAllTypes));				
	}	
	
	/**
	 * This function sends a message to server to load
	 * all sales men from DB
	 * @return servers message
	 */
	public synchronized Object loadAllSalesMen(){
		String[] loadAllSalesMen = new String[1];  
		loadAllSalesMen[0] = "LoadAllSalesMen";
		return (sendAndGetFromServer(loadAllSalesMen));				
	}	
	
	/**
	 * This function sends a message to server to load
	 * all customers for the logged in sales man from DB
	 * @param username The sales man user name
	 * @return servers message
	 */
	public synchronized Object loadAllCustomersForSalesMan(String username){

		String[] loadAllCustomersForSalesMan = new String[2];  
		loadAllCustomersForSalesMan[0] = "LoadAllCustomersForSalesMan";
		loadAllCustomersForSalesMan[1] = username;		
		return (sendAndGetFromServer(loadAllCustomersForSalesMan));				
	}	
	
	/**
	 * This function sends a message to server to update
	 * sales man schedule in DB
	 * @param schedule The new schedule
	 * @param DealSaleID The deal sale ID 
	 * @return servers message
	 */
	public synchronized Object updateSalesManSchedule(String schedule,long DealSaleID){
		String[] updateSalesManSchedule = new String[3];  
		updateSalesManSchedule[0] = "UpdateSalesManSchedule";
		updateSalesManSchedule[1] = schedule;
		updateSalesManSchedule[2] = String.valueOf(DealSaleID);
		return (sendAndGetFromServer(updateSalesManSchedule));				
	}	
	
	/**
	 * This function sends a message to server to load
	 * deal sale for customer from DB
	 * @param custID The customer ID to load
	 * @return servers message
	 */
	public synchronized Object loadDealSaleForCustomer(long custID){
		String[] loadDealSaleForCustomer = new String[2];  
		loadDealSaleForCustomer[0] = "loadDealSaleForCustomer";
		loadDealSaleForCustomer[1] = String.valueOf(custID);		
		return (sendAndGetFromServer(loadDealSaleForCustomer));				
	}	
	
	/**
	 * This function sends a message to server to save
	 * the rejected deal sale on DB
	 * @param custID The customer ID to load
	 * @param DealSaleID The rejected deal sale ID
	 * @param response The customer response
	 * @return servers message
	 */
	public synchronized Object rejectedDealSale(long custID, long DealSaleID, String response){
		String[] rejectedDealSale = new String[5];  
		rejectedDealSale[0] = "rejectedDealSale";
		rejectedDealSale[1] = String.valueOf(custID);	
		rejectedDealSale[2] = String.valueOf(DealSaleID);	
		rejectedDealSale[3] = response;	
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String datetime = dateFormat.format(date);
        rejectedDealSale[4] = datetime;	
		return (sendAndGetFromServer(rejectedDealSale));				
	}	
	
	/**
	 * This function sends a message to server to save
	 * the purchased deal sale on DB
	 * @param custID The customer ID to load
	 * @param DealSaleID The rejected deal sale ID 
	 * @return servers message
	 *
	 */
	public synchronized Object purchasedDealSale(long custID, long DealSaleID){
		String[] purchasedDealSale = new String[4];  
		purchasedDealSale[0] = "purchasedDealSale";
		purchasedDealSale[1] = String.valueOf(custID);	
		purchasedDealSale[2] = String.valueOf(DealSaleID);	
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        String datetime = dateFormat.format(date);
        purchasedDealSale[3] = datetime;	
		return (sendAndGetFromServer(purchasedDealSale));				
	}	
	
	/**
	 * This function sends a message to server to load
	 * all customers responses for a deal sale from DB
	 * @param DealSaleID The deal sale ID to load
	 * @return servers message
	 */
	public synchronized Object getResponseReport(long DealSaleID){
		String[] getResponseReport = new String[2];  
		getResponseReport[0] = "getResponseReport";		
		getResponseReport[1] = String.valueOf(DealSaleID);	
		return (sendAndGetFromServer(getResponseReport));				
	}
	
	/**
	 * This function sends a message to server to load
	 * all customer reports from DB 
	 * @return servers message
	 */
	public synchronized Object customerReport(){
		String[] customerReport = new String[1];  
		customerReport[0] = "customerReport";		
		return (sendAndGetFromServer(customerReport));				
	}	
	
	/**
	 * This function sends a message to server to start
	 * the analyzes system progres and saves the result
	 * on the DB
	 * @return servers message
	 */
	public synchronized Object Activitytracking(){
		String[] analyzesSystem = new String[1];  
		analyzesSystem[0] = "activityTracking";		
		return (sendAndGetFromServer(analyzesSystem));				
	}
	
	/**
	 * This function sends a message to server to load
	 * all market segments from DB
	 * @return servers message
	 */
	public synchronized Object loadAllMarketSegments(){
		String[] loadAllMarketSegments = new String[1];  
		loadAllMarketSegments[0] = "LoadAllMarketSegments";
		return (sendAndGetFromServer(loadAllMarketSegments));				
	}	
	
	/**
	 * This function sends a message to server to load 
	 * all customers at market segment from DB
	 * @param segmentNum The segment number
	 * @return servers message
	 */
	public synchronized Object loadAllCustomersAtMarketSegment(int segmentNum){
		String[] loadAllCustomersAtMarketSegment = new String[2];  
		loadAllCustomersAtMarketSegment[0] = "LoadAllCustomersAtMarketSegment";
		loadAllCustomersAtMarketSegment[1] = String.valueOf(segmentNum);
		return (sendAndGetFromServer(loadAllCustomersAtMarketSegment));				
	}	
	
	/**
	 * This function sends a message to server to load 
	 * all customers who are interested in the category
	 * of the prodID deal sale from DB
	 * @param prodID The product ID to load
	 * @return servers message
	 */
	public synchronized Object loadAllInterestedCustomers(long prodID){
		String[] loadAllInterestedCustomers = new String[2];  
		loadAllInterestedCustomers[0] = "LoadAllInterestedCustomers";
		loadAllInterestedCustomers[1] = String.valueOf(prodID);
		return (sendAndGetFromServer(loadAllInterestedCustomers));				
	}	
}
